<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>1_AngularJS函数库1</title>
    <script src="../lib/angularjs.js"></script>
</head>

<body ng-app="myApp" ng-controller="myCtrl"></body>

<script>
    const myApp = angular.module("myApp", []);
    myApp.controller("myCtrl", ["$scope", function ($scope) { }]);

    // 1、angular.version，查看当前的angular版本
    console.log("angular.version：", angular.version);

    // 2、angular.$$lowercase，将 string 转换为小写形式，类似原生 js 的 toLowerCase 方法
    console.log("angular.$$lowercase：", angular.$$lowercase("ABC"));

    // 3、angular.$$uppercase，将 string 转换为大写形式，类似原生 js 的 toUpperCase 方法
    console.log("angular.$$uppercase：", angular.$$uppercase("def"));

    // 4、angular.copy，主要用来复制一个 Object 或 Array。
    console.log('"angular.copy_Start"');

    let num = 123;
    let newNum = angular.copy(num);
    console.log("copy number：", newNum);

    let str = "abcdef";
    let newStr = angular.copy(str);
    console.log("copy string：", newStr);

    let bool = true;
    let newBool = angular.copy(bool);
    console.log("copy boolean：", newBool);

    let n = null;
    let newN = angular.copy(n);
    console.log("copy null：", newN);

    let u = undefined;
    let newU = angular.copy(u);
    console.log("copy undefined：", newU);

    let func = function () {
        console.log("this is function！");
    };
    let newFunc = angular.copy(func);
    console.log("copy function：", newFunc);

    let obj = { a1: "a1", b1: [1, 2, 3], c1: { cc: "cc" } };
    let newObj = angular.copy(obj);
    obj.b1[0] = {};
    console.log("copy object-newObj：", newObj, "obj：", obj);
    let newObj2 = {};
    angular.copy(obj, newObj2);
    console.log("copy obj to newObj2：", newObj2);

    let arr = [1, 2, "a", { a: 11, b: 22 }];
    let newArr = angular.copy(arr);
    arr[arr.length - 1].a = function () {
        return 111;
    };
    console.log("copy array-newArr：", newArr, "arr：", arr);
    let newArr2 = [];
    angular.copy(arr, newArr2);
    console.log("copy arr to newArr2：", newArr2);

    console.log('"angular.copy_End"');

    // 5、angular.extend，该方法主要用于多个 Object 或 Array 的合并。
    let targetObject1 = {};
    let sourceObject1 = {
        name: "海贼王",
        episode: 1000,
        combination1: [1, 2, { a: 11, b: 22 }],
        combination2: { c: 33, d: 44, e: ["a", "b", "c"] },
    };
    angular.extend(targetObject1, sourceObject1);
    targetObject1.name = "妖精的尾巴";
    targetObject1.combination1[2].a = 1122;
    console.log(
        "angular.extend-targetObject1：",
        targetObject1,
        "sourceObject1",
        sourceObject1
    );
    let targetObject2 = {};
    let sourceObject2 = {
        combination1: [111, 222, 333],
        combination2: { e: ["aaa", "bbb", "ccc"] },
    };
    angular.extend(targetObject2, sourceObject1, sourceObject2);
    console.log(
        "angular.extend-targetObject2：",
        targetObject2,
        "sourceObject1：",
        sourceObject1,
        "sourceObject2：",
        sourceObject2
    );

    let targetArray1 = [];
    let sourceArray1 = [
        111,
        222,
        333,
        { combination1: [1, 2, 3], combination2: { a: 11, b: 22, c: 33 } },
    ];
    angular.extend(targetArray1, sourceArray1);
    targetArray1[0] = "1";
    targetArray1[targetArray1.length - 1].combination1[0] = 112233;
    console.log(
        "angular.extend-targetArray1：",
        targetArray1,
        "sourceArray1：",
        sourceArray1
    );
    let targetArray2 = [];
    let sourceArray2 = [111, 444, 333];
    angular.extend(targetArray2, sourceArray1, sourceArray2);
    console.log(
        "angular.extend-targetArray2：",
        targetArray2,
        "sourceArray1：",
        sourceArray1,
        "sourceObject2：",
        sourceArray2
    );

    // 6、angular.forEach，该方法就类似原生 js 中的 forEach 方法。
    let dataArray = [1, 2, 3, 4]
    angular.forEach(dataArray, function (_, idx, array) { array[idx] = 22 })
    console.log("angular.forEach：", dataArray)
    // angular.forEach：[22, 22, 22, 22]

    // 7、angular.fromJson，该方法将 JSON 字符串转成 Object。
    let jsonStr = "{ \"name\": \"World Cup\", \"watch\": \"every day\" }"
    let jsonToObject = angular.fromJson(jsonStr)
    console.log("angular.fromJson：", jsonToObject);

    // 8、angular.toJson，该方法将 Object 转成 JSON 字符串。
    let jsonObject = { name: "World Cup", watch: "every day" }
    let objectToJson = angular.toJson(jsonObject)
    console.log("angular.toJson：", objectToJson);
</script>

</html>